package bairui.international.console.web.controller.prize.nine;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;

import bairui.international.model.domain.jpa.QTB_WinPrize;
import bairui.international.model.domain.jpa.TB_WinPrize;
import bairui.international.model.status.State_Prize;
import bairui.international.service.Service_WinPrize;

/**
 *
 * 中奖列表
 *
 * @generated
 * 
 * @see STest_C_Prize_Nine_WinPage_Page 测试
 *
 */
@Controller
public class C_Prize_Nine_WinPage_Page
 extends SC_Prize_Nine_WinPage_Page 
{
	
	@Autowired private Service_WinPrize service_WinPrize;
	
	/**
	 * 分页列表
	 */
	@SuppressWarnings("serial")
	@Action(Page.class)
	@GetMapping(value=Page.RequestMappingValue, consumes=MediaType.APPLICATION_JSON_VALUE)
	public @FomatOutput @ResponseBody Object page(
		HttpServletRequest request
	) throws Exception {
		org.springframework.data.domain.Page<TB_WinPrize> page = service_WinPrize.page(
				it->filter(it, request), 
				Page.Output.page(request), 
				it->sort(it, request)
			);
		//
		page.forEach(it->{
			it.setImgUrl(C_Prize_Nine_Detail_ShowItemImg.ShowItemImg.Operate(-1, it.getId()).getUrl());
		});
		//
		return Page.Output.output(
				page
					.map(it->new Page.Output.Model(it){
							@Override protected void assign_before() {
							}
							@Override public String assignTitle() {
								return it.getPrize().getTitle();
							}
							@Override public State_Prize assignState() {
								return it.getPrize().getState();
							}
						}
					)
			);
	}
	
	/*
	 * 过滤
	 */
	static BooleanBuilder filter(QTB_WinPrize it, HttpServletRequest request){
		return Page.Filter.query(it, request, new Page.Filter.FilterExtends(){
				@Override public Predicate gainTitle(String title) {
					return it.prize.title.like("%" + title + "%");
				}
				@Override public Predicate gainState(State_Prize state) {
					return it.prize.state.eq(state);
				}
			})
			.and(it.noPrizeFlag.isFalse());
	}
	
	/*
	 * 排序
	 */
	static Sort sort(QTB_WinPrize it, HttpServletRequest request){
		return Page.Output.sort(it, request, new Page.Output.OrderExtends(){
				@Override public Sort gainTitle(Direction direction) {
					return sort(direction, it.prize.title);
				}
				@Override public Sort gainState(Direction direction) {
					return sort(direction, it.prize.state);
				}
				@Override public Sort gainFinishor(Direction direction) {
					return sort(direction, it.finishor.name);
				}
			});
	}
	
	

}
